const $ = require('axios');
const UUID = require("uuid");
const Common = require('../common/index');
class Service extends Common {
  constructor() {
    super('SubType');
  };

  async add(ctx) {
    let data = ctx.params;
    try {
      let createTime = Date.now();
      let updateTime = Date.now();
      let res = await $.post(this.url + '/SubType', {
        id: UUID.v1(),
        createTime,
        updateTime,
        ...data,
      })
      ctx.body = {
        code: 666,
        msg: 'success',
        result: res.data
      };
    } catch (error) {
      ctx.body = {
        code: 500,
        msg: error
      }
    }
  }

  async list(ctx) {
    let {
      parentId
    } = ctx.params;
    let sort = {
      _sort: 'updateTime',
      _order: 'desc',
    }

    try {
      let res = await $.get(this.url + '/SubType', {
        params: {
          parentId,
          ...sort
        }
      });
      ctx.body = {
        code: 666,
        msg: 'success',
        result: res.data
      };
    } catch (error) {
      ctx.body = {
        code: 500,
        msg: error.message
      }
    }
  }

  async all(ctx) {
    let {
      parentId
    } = ctx.params;
    let sort = {
      _sort: 'updateTime',
      _order: 'desc',
    }
    try {
      let res = await $.get(this.url + '/SubType', {
        params: {
          parentId,
          ...sort
        }
      });
      ctx.body = {
        code: 666,
        msg: 'success',
        result: res.data
      };
    } catch (error) {
      ctx.body = {
        code: 500,
        msg: error.message
      }
    }
  }

  async edit(ctx) {
    try {
      ctx.params.updateTime = Date.now();
      let res = await $.put(this.url + '/SubType/' + ctx.params.id, ctx.params);
      ctx.body = {
        code: 666,
        msg: 'success',
        result: res.data
      }
    } catch (error) {
      ctx.body = {
        code: 500,
        msg: error.message
      }
    }
  }

  async detail(ctx) {
    try {
      let res = await $.get(this.url + '/SubType/' + ctx.params.id);
      ctx.body = {
        code: 666,
        msg: 'success',
        result: res.data
      }
    } catch (error) {
      ctx.body = {
        code: 500,
        msg: error.message
      }
    }
  }

  async del(ctx) {
    try {
      let res = await $.delete(this.url + '/SubType/' + ctx.params.id);
      ctx.body = ctx.body = {
        code: 666,
        msg: 'success',
        result: res.data
      };
    } catch (error) {
      ctx.body = {
        code: 500,
        msg: error.message
      }
    }
  }
}
module.exports = new Service();